#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QWidget>
#include <QUdpSocket>
#include <QLineEdit>
#include <QTextEdit>
#include <QPushButton>
#include <QVBoxLayout>
#include <QStatusBar>
#include <QTimerEvent>

class MainWindow : public QWidget {
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

protected:
    void timerEvent(QTimerEvent *event) override;

private slots:
    void onRegisterClicked();
    void onConnectClicked();
    void onSendClicked();
    void readPendingDatagrams();

private:
    void setupUI();
    void handleServerResponse(const QByteArray &data, const QHostAddress &sender, quint16 port);
    void handlePeerMessage(const QByteArray &data);

    QUdpSocket *udpSocket;
    QHostAddress serverAddr = QHostAddress("47.96.159.221");
    quint16 serverPort = 7561;
    QHostAddress peerAddr;
    quint16 peerPort = 0;

    QLineEdit *usernameEdit;
    QPushButton *registerBtn;
    QLineEdit *targetEdit;
    QPushButton *connectBtn;
    QTextEdit *chatDisplay;
    QLineEdit *messageEdit;
    QPushButton *sendBtn;
};

#endif // MAINWINDOW_H
